package com.iaiai.cobra.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.common.util.Md5;
import com.iaiai.cobra.common.util.StringUtil;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.repository.bean.Dept;
import com.iaiai.cobra.repository.bean.Post;
import com.iaiai.cobra.repository.bean.Role;
import com.iaiai.cobra.repository.bean.User;
import com.iaiai.cobra.repository.bean.po.UserPo;
import com.iaiai.cobra.web.params.PageParams;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created with UserController.
 * Package: com.iaiai.cobra.system.controller
 * Author: iaiai
 * Create Time: 2024/6/22 18:00
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @PostMapping("/list.json")
    public ResultVo page(@RequestBody PageParams params) {
        if(params==null)new PageParams();
        if(params.getPage()==null || params.getLimit()==null){
            return fail("参数异常");
        }

        Page<User> pages = serviceManager.userService.queryPage(new Page(params.getPage(),params.getLimit()),params.getSearch());
        Page<UserPo> newPages = new Page<>();
        newPages.setPages(pages.getPages());
        newPages.setCurrent(pages.getCurrent());
        newPages.setSize(pages.getSize());
        newPages.setTotal(pages.getTotal());
        newPages.setRecords(new ArrayList<>());
        if(pages.getRecords()!=null && pages.getRecords().size()>0){
            for (User record : pages.getRecords()) {
                record.setPassword(null);

                UserPo po = new UserPo();
                BeanUtils.copyProperties(record,po);

                po.setDepts(serviceManager.deptService.queryByUser(record.getId()));
                po.setPosts(serviceManager.postService.queryByUser(record.getId()));
                newPages.getRecords().add(po);
            }
        }

        return success(newPages);
    }

    @PostMapping("/add.do")
    public ResultVo add(@RequestBody UserPo params) {
        if(params==null
                || StringUtil.isEmpty(params.getAccount())
                || StringUtil.isEmpty(params.getPassword())
                || StringUtil.isEmpty(params.getName())
                || StringUtil.isEmpty(params.getPhone())
                || params.getGender()==null
                || params.getDisabled()==null
                || params.getRoleIds()==null
                || params.getRoleIds().size()<=0){
            return fail("参数异常");
        }

        String password = String.format("%s%s%s",projectConfig.getPasswordPrefix(),params.getPassword(),projectConfig.getPasswordSuffix());
        params.setPassword(Md5.encrypt(password));

        serviceManager.userService.add(params);
        return success();
    }

    @PostMapping("/detail.json")
    public ResultVo detail(@RequestBody User params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        Map<String, Object> map = new HashMap<>();
        User user = serviceManager.userService.getById(params.getId());
        map.put("user",user);

        List<Dept> depts = serviceManager.deptService.queryByUser(user.getId());
        map.put("depts",depts);

        List<Post> posts = serviceManager.postService.queryByUser(user.getId());
        map.put("posts",posts);

        List<Role> roles = serviceManager.roleService.queryByUser(user.getId());
        map.put("roles",roles);

        return success(map);
    }

    @PostMapping("/operate.json")
    public ResultVo operate() {
        Map<String,Object> map = new HashMap<>();

        List<Dept> depts = serviceManager.deptService.queryEnableAll();
        map.put("depts",depts);

        List<Post> posts = serviceManager.postService.queryEnableAll();
        map.put("posts",posts);

        List<Role> roles = serviceManager.roleService.queryPage(new Page(1,99999),null).getRecords();
        map.put("roles",roles);

        return success(map);
    }

    @PostMapping("/edit.do")
    public ResultVo edit(@RequestBody UserPo params) {
        if(params==null
                || params.getId()==null
                || StringUtil.isEmpty(params.getAccount())
                || StringUtil.isEmpty(params.getName())
                || StringUtil.isEmpty(params.getPhone())
                || params.getGender()==null
                || params.getDisabled()==null){
            return fail("参数异常");
        }

        serviceManager.userService.edit(params);
        return success();
    }

    @PostMapping("/edit/password.do")
    public ResultVo editPassword(@RequestBody User params) {
        if(params==null
                || params.getId()==null
                || StringUtil.isEmpty(params.getPassword())){
            return fail("参数异常");
        }

        User user = serviceManager.userService.getById(params.getId());

        String password = String.format("%s%s%s",projectConfig.getPasswordPrefix(),params.getPassword(),projectConfig.getPasswordSuffix());
        user.setPassword(Md5.encrypt(password));
        serviceManager.userService.updateById(user);
        return success();
    }

    @PostMapping("/del.do")
    public ResultVo del(@RequestBody User params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        User user = serviceManager.userService.getById(params.getId());
        user.setDel(YesOrNo.YES.getValue());
        user.setModifyTime(new Date());
        serviceManager.userService.updateById(user);
        return success();
    }

}
